<?php
require_once('common.php');
Util::cleanRequest();

if (array_key_exists("server", $_POST) || array_key_exists("clear", $_POST)) {
	Util::setCustomLoginUrl($_POST["server"], array_key_exists("clear", $_POST));
	header("Location: https://secure". SESSION_HOST . "/sfdcLogin.php?continue=1");
} else {
	$value = "";
	if (array_key_exists("custom_login_url", $_COOKIE)) {
		$value = urldecode($_COOKIE["custom_login_url"]);
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Cirrus Insight - Authorize Cirrus Insight</title>
<style type="text/css">
	body {
		margin: 0px;
		font-family:'Lucida Grande',verdana;
		background-color: #FFF;
	}

	body > div {
		margin: 20px;
		height: 375px;
	}
	
	#content {
		background-color: #ecf3ff;
		border-radius: 6px;
		margin: 10px 0px;
		padding: 10px 15px;
		height: 100%;
	}

	form {
		text-align: center;
		margin-bottom: 15px;
	}
	
	h1 {
		font-size: 22px;
		margin: 10px 0px;
	}
	
	p, p a {
		font-size: 14px;
		margin: 0 0 20px 0;
		line-height: 1.4em;
	}
	
	p.small, p.small a {
		font-size: 11px;
	}

	input.server_url {
		width: 427px;
		font-size: 1.1em;
		padding: 5px;
		float: left;
	}
	
	input.submit {
		float: right;
		margin-top: 4px;
	}
	
	br {
		clear: both;
	}
	
	input.clear {
		float: right;
		margin-top: 15px;
	}
	
	button.cancel {
		float: left;
		margin-top: 15px;
	}
	
	p.validation {
		color: #900;
		font-weight: bold;
		font-style: italic;
	}

</style>
<script type='text/javascript' src='//ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js'></script>
<script src="https://www.google-analytics.com/ga.js" type="text/javascript">
</script>
<script type="text/javascript">
  var pageTracker = _gat._getTracker("UA-24872184-1");
  pageTracker._trackPageview('/ci/sfdcLoginServer.php');

  $(document).ready(function() {
		$("form").bind("submit", (function(e) {
			if ($(e.target).className != "clear") {
				if ( $(".server_url").val().indexOf("https://") != 0 || $(".server_url").val().indexOf("my.salesforce.com") == -1) {
					$("p.validation").html("The URL entered does not appear to be valid.");
					return false;
				}
			}
			return true;
		}));

		$("input.server_url").bind("focus", (function() {
			$("p.validation").html("&nbsp;");
		}));
  });
</script>
</head>

<body>

	<div>
		<a href="https://www.cirrusinsight.com">
			<img alt="Cirrus Insight" src="http://www.cirrusinsight.com/wp-content/uploads/2011/12/logo_full_45h.png" width="289" height="45" />
		</a>	
	
		<div id="content">
			<h1>Cirrus Insight Custom Login</h1>
			<p>If you use Single Sign-On or the My Domains features with salesforce.com, please enter the URL of your custom salesforce.com login page below.</p> 

			<form method="post">
				<p class="validation">&nbsp;</p>
				<input type="text" class="server_url" name="server" placeholder="e.g. https://your-company.my.salesforce.com/" value="<?=$value?>" />
				<input type="image" src="//www.cirrusinsight.com/wp-content/uploads/2011/12/secure-signin-small.png" class="submit" /><br/>
				<button onclick="location.href='/sfdcLogin.php'" class="cancel">Cancel</button>
				<input type="submit" value="Clear URL" name="clear" value="1" class="clear" />
			</form>
			
			<br/><br/>		
			<p>By clicking sign in, you agree to the Cirrus Insight <a href="http://www.cirrusinsight.com/terms" target="_blank">Terms of Service</a>.</p>					
			<p>We never see or store your password. All data is secured with SSL. For more information, please see our <a href="http://www.cirrusinsight.com/privacy" target="_blank">Privacy Policy</a>. If you have any questions, please <a href="http://www.cirrusinsight.com/contact" target="_blank">contact us</a>.</p>
		</div>

	</div>

</body>
</html>
<?php } ?>